package com.alex.books;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import android.content.Context;
import android.database.Cursor;

import com.alex.database.BookDao;
import com.alex.database.BookDatabaseHelper.Book_TB;
import com.alex.database.BookDatabaseHelper.Bookmark_TB;
import com.alex.database.BookDatabaseHelper.Chapter_TB;

public class BookStore {

	public String DBPath = "/sdcard/";
	
	private static BookStore instance;
	
	public static BookStore getInstance(){
		if(instance == null){
			instance = new BookStore();
		}
		return instance;
	}
	
	/**
	 * 获取某个本书
	 * @param context
	 * @param bookId
	 * @return
	 */
	public BaseBook getABook(Context context, int bookId){
		BaseBook baseBook = new BaseBook();
        Cursor mCursor = BookDao.getInstance().queryBook(context,bookId);
        if(mCursor.moveToFirst()){
        	baseBook.mBookId = mCursor.getInt(mCursor.getColumnIndex(Book_TB._ID));
        	baseBook.mBookName = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_NAME));
        	baseBook.mBookSerial = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_SERIAL));
        	baseBook.mBookPath = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_PATH));
        	baseBook.mBookAuthor = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_AUTHOER));
        	baseBook.mWordNumber = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_WORDNUMBER));
        	baseBook.mChapterNumber = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_CHAPTERNUMBER));
        	baseBook.mSource = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_SOURCE));
        	baseBook.mAboutAuthor = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_ABOUTAUTHOR));
        	baseBook.mAboutBook = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_ABOUTBOOK));
        	baseBook.mBookCover = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_COVERURL));
        	baseBook.mIsFirstReader = mCursor.getInt(mCursor.getColumnIndex(Book_TB.BOOK_ISFIRSTREAD));
        	baseBook.mAttach1 = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_ATTACH1));
        	baseBook.mAttach2 = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOK_ATTACH2));
        	baseBook.mLastTime = mCursor.getString(mCursor.getColumnIndex(Book_TB.BOOk_LASTTIME));
        	baseBook.mBookMarksNumber = mCursor.getInt(mCursor.getColumnIndex(Book_TB.BOOK_MARKSNUMBER));
        	ReaderingInfo readeringInfo = new ReaderingInfo();
        	
        	readeringInfo.mMarkPos = mCursor.getInt(mCursor.getColumnIndex(Book_TB.BOOK_MARK));
        	readeringInfo.mIndex = mCursor.getInt(mCursor.getColumnIndex(Book_TB.BOOK_CHAPTER_INDEX));
        	readeringInfo.mCurChapter.mBookId = baseBook.mBookId;
        	readeringInfo.mCurChapter.id = mCursor.getInt(mCursor.getColumnIndex(Book_TB.CHAPTER_ID));
        	readeringInfo.mCurChapter.mChapterIndex = mCursor.getInt(mCursor.getColumnIndex(Book_TB.CHAPTER_INDEX));
        	readeringInfo.mCurChapter.mChapterName = mCursor.getString(mCursor.getColumnIndex(Book_TB.CHAPTER_NAME));
        	readeringInfo.mCurChapter.mPosBegin = mCursor.getInt(mCursor.getColumnIndex(Book_TB.CHAPTER_BEGIN));
        	readeringInfo.mCurChapter.mPosEnd = mCursor.getInt(mCursor.getColumnIndex(Book_TB.CHAPTER_END));
        	readeringInfo.mCurChapter.mLength = mCursor.getInt(mCursor.getColumnIndex(Book_TB.CHAPTER_LENGTH));
        	baseBook.mReaderingInfo = readeringInfo;
        }
        mCursor.close();
        return baseBook;
	}
	
	public Vector<BaseBook> getBooks(Context context,String chars,String orderBy){
		Vector<BaseBook> books = new Vector<BaseBook>();
		Cursor c = BookDao.getInstance().queryBooks(context,chars,orderBy);
		while(c.moveToNext()){
			BaseBook book = new BaseBook();
			book.mBookId =  c.getInt(c.getColumnIndex(Book_TB._ID));
			book.mBookName = c.getString(c.getColumnIndex(Book_TB.BOOK_NAME));
			book.mBookSerial = c.getString(c.getColumnIndex(Book_TB.BOOK_SERIAL));
			book.mBookPath = c.getString(c.getColumnIndex(Book_TB.BOOK_PATH));
			book.mBookAuthor = c.getString(c.getColumnIndex(Book_TB.BOOK_AUTHOER));
			book.mWordNumber = c.getString(c.getColumnIndex(Book_TB.BOOK_WORDNUMBER));
			book.mChapterNumber = c.getString(c.getColumnIndex(Book_TB.BOOK_CHAPTERNUMBER));
			book.mSource = c.getString(c.getColumnIndex(Book_TB.BOOK_SOURCE));
			book.mAboutAuthor = c.getString(c.getColumnIndex(Book_TB.BOOK_ABOUTAUTHOR));
			book.mAboutBook = c.getString(c.getColumnIndex(Book_TB.BOOK_ABOUTBOOK));
			book.mBookCover = c.getString(c.getColumnIndex(Book_TB.BOOK_COVERURL));
			book.mAttach1 = c.getString(c.getColumnIndex(Book_TB.BOOK_ATTACH1));
			book.mAttach2 = c.getString(c.getColumnIndex(Book_TB.BOOK_ATTACH2));
			book.mLastTime = c.getString(c.getColumnIndex(Book_TB.BOOk_LASTTIME));
			book.mBookMarksNumber = c.getInt(c.getColumnIndex(Book_TB.BOOK_MARKSNUMBER));
			book.mIsFirstReader = c.getInt(c.getColumnIndex(Book_TB.BOOK_ISFIRSTREAD));
			book.mReaderingInfo.mMarkPos = c.getInt(c.getColumnIndex(Book_TB.BOOK_MARK));
			book.mReaderingInfo.mCurChapter.id = c.getInt(c.getColumnIndex(Book_TB.CHAPTER_ID));
			book.mReaderingInfo.mCurChapter.mChapterIndex = c.getInt(c.getColumnIndex(Book_TB.CHAPTER_INDEX));
        	book.mReaderingInfo.mCurChapter.mChapterName = c.getString(c.getColumnIndex(Book_TB.CHAPTER_NAME));
        	book.mReaderingInfo.mCurChapter.mPosBegin = c.getInt(c.getColumnIndex(Book_TB.CHAPTER_BEGIN));
        	book.mReaderingInfo.mCurChapter.mPosEnd = c.getInt(c.getColumnIndex(Book_TB.CHAPTER_END));
        	book.mReaderingInfo.mCurChapter.mLength = c.getInt(c.getColumnIndex(Book_TB.CHAPTER_LENGTH));
			books.add(book);
		}
		c.close();
		return books;
	}
	
	public Vector<Chapter> getChapters(Context context,int bookId){
		Vector<Chapter> chapters = new Vector<Chapter>();
		Cursor c = BookDao.getInstance().queryChapter(context,bookId);
		while(c.moveToNext()){
			Chapter chapter = new Chapter();
			chapter.id = c.getInt(c.getColumnIndex(Chapter_TB._ID));
			chapter.mBookId = c.getInt(c.getColumnIndex(Chapter_TB.BOOK_ID));
			chapter.mChapterIndex = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_INDEX));
			chapter.mChapterName = c.getString(c.getColumnIndex(Chapter_TB.CHAPTER_NAME));
			chapter.mPosBegin = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_BEGINPOS));
			chapter.mPosEnd = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_ENDPOS));
			chapter.mLength  = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_LENGTH));
			chapters.add(chapter);
		}
		c.close();
		return chapters;
	}
	
	public Vector<BookMark> getBookMarks(Context context,int bookId){
		Vector<BookMark> bookMarks = new Vector<BookMark>();
		Cursor c = BookDao.getInstance().queryBookMarks(context,bookId);
		while(c.moveToNext()){
			BookMark bookMark = new BookMark();
			bookMark.id = c.getInt(c.getColumnIndex(Bookmark_TB._ID));
			bookMark.mBookId = c.getInt(c.getColumnIndex(Bookmark_TB.BOOK_ID));
			bookMark.mName = c.getString(c.getColumnIndex(Bookmark_TB.BOOKMARK_NAME));
			bookMark.mTime = c.getString(c.getColumnIndex(Bookmark_TB.BOOkMARK_TIME));
			bookMark.mMarkPos = c.getInt(c.getColumnIndex(Bookmark_TB.BOOKMARK_MARK));
			bookMark.mIntro = c.getString(c.getColumnIndex(Bookmark_TB.BOOKMARK_INTRO));
			bookMark.mChapter.id = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_ID));
			bookMark.mChapter.mChapterIndex = c.getInt(c.getColumnIndex(Book_TB.CHAPTER_INDEX));
			bookMark.mChapter.mChapterName = c.getString(c.getColumnIndex(Bookmark_TB.CHAPTER_NAME));
			bookMark.mChapter.mPosBegin = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_BEGIN));
			bookMark.mChapter.mPosEnd = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_END));
			bookMark.mChapter.mLength = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_LENGTH));
			bookMarks.add(bookMark);
		}
		c.close();
		return bookMarks;
	}
	
	public long addBookMark(Context context,BaseBook book,String info){
		Date date = new Date(System.currentTimeMillis());
		 SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		BookMark bookMark = new BookMark();
		bookMark.mBookId = book.mBookId;
		bookMark.mMarkPos = (int)book.mReaderingInfo.mMarkPos;
		bookMark.mIntro = info;
		bookMark.mChapter = BookPageFactory.getInstance().getCurChapter();
		bookMark.mTime = format1.format(date);
		return BookDao.getInstance().insertBookmark(context, bookMark);
	}
	
	public void deleteBook(Context context,BaseBook book){
		BookDao.getInstance().deleteBook(context, book);
	}
	
	public long addBook(Context context, BaseBook book){
		long id = 0L;
		BookDao.getInstance().insertBook(context, book);
		return id;
	}
	
	public void setHasRead(Context context, BaseBook book){
		book.mIsFirstReader = 0;
		BookDao.getInstance().updateBook(context, book);
	}
}
